1 using System;
2
3 namespace ProceduralToolkit
4 {
5 /// <summary>
6 /// Representation of 2D vectors and points using integers
7 /// </summary>
8 [Serializable]
9 public struct Vector2Int
10 {
11 /// <summary>
12 /// X component of the vector
13 /// </summary>
14 public int x;
15
16 /// <summary>
17 /// Y component of the vector
18 /// </summary>
19 public int y;
20
21 #region Static constructors
22
23 /// <summary>
24 /// Shorthand for writing new Vector2Int(0, 0)
25 /// </summary>
26 public static Vector2Int zero { get { return new Vector2Int(0, 0); } }
27 /// <summary>
28 /// Shorthand for writing new Vector2Int(1, 1)
29 /// </summary>
30 public static Vector2Int one { get { return new Vector2Int(1, 1); } }
31 /// <summary>
32 /// Shorthand for writing new Vector2Int(1, 0)
33 /// </summary>
34 public static Vector2Int right { get { return new Vector2Int(1, 0); } }
35 /// <summary>
36 /// Shorthand for writing new Vector2Int(-1, 0)
37 /// </summary>
38 public static Vector2Int left { get { return new Vector2Int(-1, 0); } }
39 /// <summary>
40 /// Shorthand for writing new Vector2Int(0, 1)
41 /// </summary>
42 public static Vector2Int up { get { return new Vector2Int(0, 1); } }
43 /// <summary>
44 /// Shorthand for writing new Vector2Int(0, -1)
45 /// </summary>
46 public static Vector2Int down { get { return new Vector2Int(0, -1); } }
47
48 #endregion Static constructors
49
50 /// <summary>
51 /// Returns the length of this vector (RO)
52 /// </summary>
53 public int magnitude { get { return (int) Math.Sqrt(sqrMagnitude); } }
54
55 /// <summary>
56 /// Returns the squared length of this vector (RO)
57 /// </summary>
58 public int sqrMagnitude { get { return x*x + y*y; } }
59
60 /// <summary>
61 /// Returns this vector with a magnitude of 1 (RO)
62 /// </summary>
63 public Vector2Int normalized
64 {
65 get
66 {
67 var vector = new Vector2Int(x, y);
68 vector.Normalize();
69 return vector;
70 }
71 }
72
73 /// <summary>
74 /// Constructs a new vector with given x, y components
75 /// </summary>
76 public Vector2Int(int x, int y)
77 {
78 this.x = x;
79 this.y = y;
80 }
81
82 /// <summary>
83 /// Makes this vector have a magnitude of 1
84 /// </summary>
85 public void Normalize()
86 {
87 int magnitude = this.magnitude;
88 if (magnitude > 0)
89 {
90 this /= magnitude;
91 }
92 else
93 {
94 this = zero;
95 }
96 }
97
98 /// <summary>
99 /// Dot Product of two vectors
100 /// </summary>
101 public static int Dot(Vector2Int lhs, Vector2Int rhs)
102 {
103 return lhs.x*rhs.x + lhs.y*rhs.y;
104 }
105
106 #region Operators
107
108 public static Vector2Int operator +(Vector2Int a, Vector2Int b)
109 {
110 return new Vector2Int(a.x + b.x, a.y + b.y);
111 }
112
113 public static Vector2Int operator -(Vector2Int a, Vector2Int b)
114 {
115 return new Vector2Int(a.x - b.x, a.y - b.y);
116 }
117
118 public static Vector2Int operator -(Vector2Int a)
119 {
120 return new Vector2Int(-a.x, -a.y);
121 }
122
123 public static Vector2Int operator *(int d, Vector2Int a)
124 {
125 return new Vector2Int(a.x*d, a.y*d);
126 }
127
128 public static Vector2Int operator *(Vector2Int a, int d)
129 {
130 return new Vector2Int(a.x*d, a.y*d);
131 }
132
133 public static Vector2Int operator /(Vector2Int a, int d)
134 {
135 return new Vector2Int(a.x/d, a.y/d);
136 }
137
138 public static bool operator ==(Vector2Int a, Vector2Int b)
139 {
140 return a.x == b.x && a.y == b.y;
141 }
142
143 public static bool operator !=(Vector2Int a, Vector2Int b)
144 {
145 return !(a == b);
146 }
147
148 #endregion Operators
149
150 public override int GetHashCode()
151 {
152 return x.GetHashCode() ^ y.GetHashCode() << 2;
153 }
154
155 public override bool Equals(object other)
156 {
157 if (!(other is Vector2Int))
158 {
159 return false;
160 }
161 Vector2Int vector2Int = (Vector2Int) other;
162 if (x.Equals(vector2Int.x))
163 {
164 return y.Equals(vector2Int.y);
165 }
166 return false;
167 }
168
169 /// <summary>
170 /// Returns a nicely formatted string for this vector
171 /// </summary>
172 public override string ToString()
173 {
174 return string.Format("({0}, {1})", x, y);
175 }
176 }
177 }